<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>filereader_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_testing_fs_filereader_test.js.html">filereader_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2011 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.testing.fs.FileReaderTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.testing.fs.FileReaderTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.Timer&#39;);
<a name="line19"></a>goog.require(&#39;goog.async.Deferred&#39;);
<a name="line20"></a>goog.require(&#39;goog.events&#39;);
<a name="line21"></a>goog.require(&#39;goog.fs.Error&#39;);
<a name="line22"></a>goog.require(&#39;goog.fs.FileReader&#39;);
<a name="line23"></a>goog.require(&#39;goog.fs.FileSaver&#39;);
<a name="line24"></a>goog.require(&#39;goog.testing.AsyncTestCase&#39;);
<a name="line25"></a>goog.require(&#39;goog.testing.fs.FileReader&#39;);
<a name="line26"></a>goog.require(&#39;goog.testing.fs.FileSystem&#39;);
<a name="line27"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line28"></a>
<a name="line29"></a>var asyncTestCase = goog.testing.AsyncTestCase.createAndInstall();
<a name="line30"></a>var file, deferredReader;
<a name="line31"></a>var hasArrayBuffer = goog.isDef(goog.global.ArrayBuffer);
<a name="line32"></a>
<a name="line33"></a>function setUp() {
<a name="line34"></a>  var fs = new goog.testing.fs.FileSystem();
<a name="line35"></a>  var fileEntry = fs.getRoot().createDirectorySync(&#39;foo&#39;).createFileSync(&#39;bar&#39;);
<a name="line36"></a>  file = fileEntry.fileSync();
<a name="line37"></a>  file.setDataInternal(&#39;test content&#39;);
<a name="line38"></a>
<a name="line39"></a>  deferredReader = new goog.async.Deferred();
<a name="line40"></a>  goog.Timer.callOnce(
<a name="line41"></a>      goog.bind(deferredReader.callback, deferredReader,
<a name="line42"></a>          new goog.testing.fs.FileReader()));
<a name="line43"></a>}
<a name="line44"></a>
<a name="line45"></a>function testRead() {
<a name="line46"></a>  deferredReader.
<a name="line47"></a>      addCallback(goog.partial(checkReadyState,
<a name="line48"></a>                               goog.fs.FileReader.ReadyState.INIT)).
<a name="line49"></a>      addCallback(goog.partial(checkResult, undefined)).
<a name="line50"></a>      addCallback(goog.partial(readAsText)).
<a name="line51"></a>      addCallback(goog.partial(checkReadyState,
<a name="line52"></a>                               goog.fs.FileReader.ReadyState.LOADING)).
<a name="line53"></a>      addCallback(goog.partial(checkResult, undefined)).
<a name="line54"></a>      addCallback(goog.partial(waitForEvent,
<a name="line55"></a>                               goog.fs.FileReader.EventType.LOAD_START)).
<a name="line56"></a>      addCallback(goog.partial(checkResult, undefined)).
<a name="line57"></a>      addCallback(goog.partial(waitForEvent,
<a name="line58"></a>                               goog.fs.FileReader.EventType.LOAD)).
<a name="line59"></a>      addCallback(goog.partial(checkResult, undefined)).
<a name="line60"></a>      addCallback(goog.partial(waitForEvent,
<a name="line61"></a>                               goog.fs.FileReader.EventType.LOAD_END)).
<a name="line62"></a>      addCallback(goog.partial(checkResult, file.toString())).
<a name="line63"></a>      addCallback(goog.partial(checkReadyState,
<a name="line64"></a>                               goog.fs.FileReader.ReadyState.DONE)).
<a name="line65"></a>      addBoth(continueTesting);
<a name="line66"></a>  waitForAsync(&#39;testRead&#39;);
<a name="line67"></a>}
<a name="line68"></a>
<a name="line69"></a>function testReadAsArrayBuffer() {
<a name="line70"></a>  if (!hasArrayBuffer) {
<a name="line71"></a>    // Skip if array buffer is not supported
<a name="line72"></a>    return;
<a name="line73"></a>  }
<a name="line74"></a>  deferredReader.
<a name="line75"></a>      addCallback(goog.partial(checkReadyState,
<a name="line76"></a>                               goog.fs.FileReader.ReadyState.INIT)).
<a name="line77"></a>      addCallback(goog.partial(checkResult, undefined)).
<a name="line78"></a>      addCallback(goog.partial(readAsArrayBuffer)).
<a name="line79"></a>      addCallback(goog.partial(checkReadyState,
<a name="line80"></a>                               goog.fs.FileReader.ReadyState.LOADING)).
<a name="line81"></a>      addCallback(goog.partial(checkResult, undefined)).
<a name="line82"></a>      addCallback(goog.partial(waitForEvent,
<a name="line83"></a>                               goog.fs.FileReader.EventType.LOAD_START)).
<a name="line84"></a>      addCallback(goog.partial(checkResult, undefined)).
<a name="line85"></a>      addCallback(goog.partial(waitForEvent,
<a name="line86"></a>                               goog.fs.FileReader.EventType.LOAD)).
<a name="line87"></a>      addCallback(goog.partial(checkResult, undefined)).
<a name="line88"></a>      addCallback(goog.partial(waitForEvent,
<a name="line89"></a>                               goog.fs.FileReader.EventType.LOAD_END)).
<a name="line90"></a>      addCallback(goog.partial(checkResult, file.toArrayBuffer())).
<a name="line91"></a>      addCallback(goog.partial(checkReadyState,
<a name="line92"></a>                               goog.fs.FileReader.ReadyState.DONE)).
<a name="line93"></a>      addBoth(continueTesting);
<a name="line94"></a>  waitForAsync(&#39;testReadAsArrayBuffer&#39;);
<a name="line95"></a>}
<a name="line96"></a>
<a name="line97"></a>function testReadAsDataUrl() {
<a name="line98"></a>  deferredReader.
<a name="line99"></a>      addCallback(goog.partial(checkReadyState,
<a name="line100"></a>                               goog.fs.FileReader.ReadyState.INIT)).
<a name="line101"></a>      addCallback(goog.partial(checkResult, undefined)).
<a name="line102"></a>      addCallback(goog.partial(readAsDataUrl)).
<a name="line103"></a>      addCallback(goog.partial(checkReadyState,
<a name="line104"></a>                               goog.fs.FileReader.ReadyState.LOADING)).
<a name="line105"></a>      addCallback(goog.partial(checkResult, undefined)).
<a name="line106"></a>      addCallback(goog.partial(waitForEvent,
<a name="line107"></a>                               goog.fs.FileReader.EventType.LOAD_START)).
<a name="line108"></a>      addCallback(goog.partial(checkResult, undefined)).
<a name="line109"></a>      addCallback(goog.partial(waitForEvent,
<a name="line110"></a>                               goog.fs.FileReader.EventType.LOAD)).
<a name="line111"></a>      addCallback(goog.partial(checkResult, undefined)).
<a name="line112"></a>      addCallback(goog.partial(waitForEvent,
<a name="line113"></a>                               goog.fs.FileReader.EventType.LOAD_END)).
<a name="line114"></a>      addCallback(goog.partial(checkResult, file.toDataUrl())).
<a name="line115"></a>      addCallback(goog.partial(checkReadyState,
<a name="line116"></a>                               goog.fs.FileReader.ReadyState.DONE)).
<a name="line117"></a>      addBoth(continueTesting);
<a name="line118"></a>  waitForAsync(&#39;testReadAsDataUrl&#39;);
<a name="line119"></a>}
<a name="line120"></a>
<a name="line121"></a>function testAbort() {
<a name="line122"></a>  deferredReader.
<a name="line123"></a>      addCallback(goog.partial(readAsText)).
<a name="line124"></a>      addCallback(function(reader) { reader.abort(); }).
<a name="line125"></a>      addCallback(goog.partial(checkReadyState,
<a name="line126"></a>                               goog.fs.FileReader.ReadyState.LOADING)).
<a name="line127"></a>      addCallback(goog.partial(waitForError, goog.fs.Error.ErrorCode.ABORT)).
<a name="line128"></a>      addCallback(goog.partial(checkReadyState,
<a name="line129"></a>                               goog.fs.FileReader.ReadyState.LOADING)).
<a name="line130"></a>      addCallback(goog.partial(waitForEvent,
<a name="line131"></a>                               goog.fs.FileReader.EventType.ABORT)).
<a name="line132"></a>      addCallback(goog.partial(checkReadyState,
<a name="line133"></a>                               goog.fs.FileReader.ReadyState.LOADING)).
<a name="line134"></a>      addCallback(goog.partial(waitForEvent,
<a name="line135"></a>                               goog.fs.FileReader.EventType.LOAD_END)).
<a name="line136"></a>      addCallback(goog.partial(checkReadyState,
<a name="line137"></a>                               goog.fs.FileReader.ReadyState.DONE)).
<a name="line138"></a>      addCallback(goog.partial(checkResult, undefined)).
<a name="line139"></a>      addBoth(continueTesting);
<a name="line140"></a>  waitForAsync(&#39;testAbort&#39;);
<a name="line141"></a>}
<a name="line142"></a>
<a name="line143"></a>function testAbortBeforeRead() {
<a name="line144"></a>  deferredReader.
<a name="line145"></a>      addCallback(function(reader) { reader.abort(); }).
<a name="line146"></a>      addErrback(function(err) {
<a name="line147"></a>        assertEquals(goog.fs.Error.ErrorCode.INVALID_STATE, err.code);
<a name="line148"></a>        return true;
<a name="line149"></a>      }).
<a name="line150"></a>      addCallback(function(calledErrback) {
<a name="line151"></a>        assertTrue(calledErrback);
<a name="line152"></a>      }).
<a name="line153"></a>      addBoth(continueTesting);
<a name="line154"></a>  waitForAsync(&#39;testAbortBeforeRead&#39;);
<a name="line155"></a>}
<a name="line156"></a>
<a name="line157"></a>function testReadDuringRead() {
<a name="line158"></a>  deferredReader.
<a name="line159"></a>      addCallback(goog.partial(readAsText)).
<a name="line160"></a>      addCallback(goog.partial(readAsText)).
<a name="line161"></a>      addErrback(function(err) {
<a name="line162"></a>        assertEquals(goog.fs.Error.ErrorCode.INVALID_STATE, err.code);
<a name="line163"></a>        return true;
<a name="line164"></a>      }).
<a name="line165"></a>      addCallback(assertTrue).
<a name="line166"></a>      addBoth(continueTesting);
<a name="line167"></a>  waitForAsync(&#39;testReadDuringRead&#39;);
<a name="line168"></a>}
<a name="line169"></a>
<a name="line170"></a>function continueTesting(result) {
<a name="line171"></a>  asyncTestCase.continueTesting();
<a name="line172"></a>  if (result instanceof Error) {
<a name="line173"></a>    throw result;
<a name="line174"></a>  }
<a name="line175"></a>}
<a name="line176"></a>
<a name="line177"></a>function waitForAsync(msg) {
<a name="line178"></a>  asyncTestCase.waitForAsync(msg);
<a name="line179"></a>}
<a name="line180"></a>
<a name="line181"></a>function waitForEvent(type, target) {
<a name="line182"></a>  var d = new goog.async.Deferred();
<a name="line183"></a>  goog.events.listenOnce(target, type, goog.bind(d.callback, d, target));
<a name="line184"></a>  return d;
<a name="line185"></a>}
<a name="line186"></a>
<a name="line187"></a>function waitForError(type, target) {
<a name="line188"></a>  var d = new goog.async.Deferred();
<a name="line189"></a>  goog.events.listenOnce(
<a name="line190"></a>      target, goog.fs.FileReader.EventType.ERROR, function(e) {
<a name="line191"></a>        assertEquals(type, target.getError().code);
<a name="line192"></a>        d.callback(target);
<a name="line193"></a>      });
<a name="line194"></a>  return d;
<a name="line195"></a>}
<a name="line196"></a>
<a name="line197"></a>function readAsText(reader) {
<a name="line198"></a>  reader.readAsText(file);
<a name="line199"></a>}
<a name="line200"></a>
<a name="line201"></a>function readAsArrayBuffer(reader) {
<a name="line202"></a>  reader.readAsArrayBuffer(file);
<a name="line203"></a>}
<a name="line204"></a>
<a name="line205"></a>function readAsDataUrl(reader) {
<a name="line206"></a>  reader.readAsDataUrl(file);
<a name="line207"></a>}
<a name="line208"></a>
<a name="line209"></a>function readAndWait(reader) {
<a name="line210"></a>  readAsText(reader);
<a name="line211"></a>  return waitForEvent(goog.fs.FileSaver.EventType.LOAD_END, reader);
<a name="line212"></a>}
<a name="line213"></a>
<a name="line214"></a>function checkResult(expectedResult, reader) {
<a name="line215"></a>  checkEquals(expectedResult, reader.getResult());
<a name="line216"></a>}
<a name="line217"></a>
<a name="line218"></a>function checkEquals(a, b) {
<a name="line219"></a>  if (hasArrayBuffer &amp;&amp;
<a name="line220"></a>      a instanceof ArrayBuffer &amp;&amp; b instanceof ArrayBuffer) {
<a name="line221"></a>    assertEquals(a.byteLength, b.byteLength);
<a name="line222"></a>    var viewA = new Uint8Array(a);
<a name="line223"></a>    var viewB = new Uint8Array(b);
<a name="line224"></a>    for (var i = 0; i &lt; a.byteLength; i++) {
<a name="line225"></a>      assertEquals(viewA[i], viewB[i]);
<a name="line226"></a>    }
<a name="line227"></a>  } else {
<a name="line228"></a>    assertEquals(a, b);
<a name="line229"></a>  }
<a name="line230"></a>}
<a name="line231"></a>
<a name="line232"></a>function checkReadyState(expectedState, reader) {
<a name="line233"></a>  assertEquals(expectedState, reader.getReadyState());
<a name="line234"></a>}
</pre>


</body>
</html>
